package com.sparrow.controller.index;

import com.sparrow.bean.Test;
import com.sparrow.dao.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by wancheng on 2019/1/16.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    UserRepository userRepository;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping(name = "首页",value = "/index",method = RequestMethod.GET)
    public String index(ModelAndView model)  {
        return "index";
    }

    @RequestMapping(name = "页面测试",value = "/getThymeleaf",method = RequestMethod.GET)
    public String getThymeleaf(Model model)  {
        Test user =  userRepository.getOne(3);
        model.addAttribute("name",user.getUserName());
        return "test";
    }

    @RequestMapping(name = "添加用户",value = "/addUser",method = RequestMethod.GET)
    @ResponseBody
    public String addUser(String userName)  {
        Test user = new Test();
        user.setUserName(userName);
        userRepository.save(user);
        return "添加成功"+userName;
    }
    @RequestMapping(name = "redis测试",value = "/redisTest",method = RequestMethod.GET)
    public String redisTest(Model model)  {
        stringRedisTemplate.opsForValue().set("qwe","qwe");
        model.addAttribute("name",stringRedisTemplate.opsForValue().get("qwe"));
        return "test";
    }
}
